﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class OutBackMatAdd : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName = "BsOutbackmat" + DateTime.Now.ToString("yyyyMMdd");
        private string m_strTbl = "BS_OutBackMat";
        private string m_strMainKey = "BillID";

        private DataTable DtOutBackMatData
        {
            get
            {
                if (null == Session["OutBackMatData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["OutBackMatData"];
                }
            }
            set
            {
                Session["OutBackMatData"] = value;
            }
        }

        private DataTable CreateDT()
        {
           
            return  BsOutbackmat.CreateDT();
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtOutBackMatData = null;
            SetControlAttr(new BsOutbackmatEntity(), tBase);
            lblBillID.Text = ClsComm.GetBillCode(m_strTbl, m_strMainKey, "WB" + DateTime.Now.ToString("yyMMdd"), 3);
            lblBillDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

           
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
        }

       

      

        private void BindData()
        {
            
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }

      

       

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                return;
            }

            BsOutbackmatEntity entity = new BsOutbackmatEntity();
            entity.OutOrderID = GetString( txtOutOrderID);

            entity.WorkOrderNo = GetString(txtWorkOrderNo);
            entity.SupplierID = GetString(hidSupplierID);
            entity.MaterialID = GetString(txtMaterialID);
            entity.Nums = GetDecimal(txtNums);
            entity.IsRepair = CommonMethod.GetString(hidIsRepair.Value); 

            entity.BillDate = GetDateTime(lblBillDate);
            entity.BillID = GetString(lblBillID);
            entity.Status = "1";
            entity.CreateUserID = CurrentUser.UserID;
            entity.CreateUserName = CurrentUser.UserName;
            entity.CreateDate = DateTime.Now;
           
          



            BsOutbackmat.SaveOrUpdate(entity,true);
            base.LogAction(BaseEnum.ActionType.Add, entity.BillID, true, null, new EntityBase[] { entity });
            DtOutBackMatData = null;
            UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../OutOrder/OutBackMatList.aspx");
        }

      

      

      
        protected void btnSelectOutOrderID_Click(object sender, EventArgs e)
        {
            BindDetailData();

        }
        /// <summary>
        /// 取得内容，填到表格
        /// </summary>
        private void BindDetailData()
        {
            string strOutOrderID = hidOutOrderID.Value.Trim();

            if (string.IsNullOrEmpty(strOutOrderID))
            {
                return;
            }

             DataTable dtTemp = CreateDT();


             bool isRepair = CommonMethod.GetString(hidIsRepair.Value).Equals("1"); 
            DataTable dt = BsOutbackmat.SelectOutProcessDetail(isRepair,  lblBillID.Text, strOutOrderID);
          
            if (dt.Rows.Count != 0)
            {

                DataRow dr = dt.Rows[0];
                txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
                txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
                hidSupplierID.Value = CommonMethod.GetString(dr["SupplierID"]);
                txtSupplierName.Text = CommonMethod.GetString(dr["SupplierName"]);
                txtOutOrderID.Text = strOutOrderID;
                txtBackProcessID.Text = CommonMethod.GetString(dr["BackProcessID"]);
                txtNums.Text = CommonMethod.GetString(dr["CanBackNum"]);
                txtMaterialID.Text = CommonMethod.GetString(dr["BackMaterialID"]);

                txtOutProcessID.Text = CommonMethod.GetString(dr["OutProcessID"]);
                txtMaterialName.Text = CommonMethod.GetString(dr["MaterialName"]);
                txtSpecifications.Text = CommonMethod.GetString(dr["Specifications"]);
                txtUnit.Text = CommonMethod.GetString(dr["Unit"]);
                txtCanBackNum.Text = CommonMethod.GetString(dr["CanBackNum"]);
            }
            BindData();
        }

      

        #endregion

        #region 本地方法
        private bool ValidateData()
        {
            string strMention = "";


            if (ClsComm.CheckExist(m_strTbl, m_strMainKey, lblBillID.Text.Trim(), true))
                {
                    strMention = "委外件回厂编号已经存在！<br />";
                }

                

                decimal sum =CommonMethod.ConvertToDecimal(txtNums.Text,2) ;
               
                if (sum<=0)
                {
                    strMention = "回厂数量不可为空！<br />";
                }
               
            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }
        #endregion


    }
}

